<script setup lang="tsx">
import { ContentWrap } from '@/components/ContentWrap'
import { Search } from '@/components/Search'
import { useI18n } from '@/hooks/web/useI18n'
import { Table } from '@/components/Table'
import { getListApi, delListApi, getFieldEnumApi } from '@/api/customer'
import { FieldEnum, SchemaConfig } from '@/api/customer/types'
import { useTable } from '@/hooks/web/useTable'
import { ref, unref } from 'vue'
import { useRouter } from 'vue-router'
import { useEventBus } from '@/hooks/event/useEventBus'
import { useCrudSchemas, AllSchemas } from '@/hooks/web/useCrudSchemas'
import { BaseButton } from '@/components/Button'
import useSchema from './components/hooks/useSchema'
import { getSelectListApi as getOperatorSelectListApi } from '@/api/operator'
import { SelectOption } from '@/api/global/types'

defineOptions({
  name: 'CustomerPage'
})

const { push } = useRouter()

const ids = ref<string[]>([])

const searchParams = ref({})
const setSearchParams = (params: any) => {
  searchParams.value = params
  getList()
}

const { tableRegister, tableState, tableMethods } = useTable({
  fetchDataApi: async () => {
    const { currentPage, pageSize } = tableState
    console.log(1)
    const res = await getListApi({
      pageIndex: unref(currentPage),
      pageSize: unref(pageSize),
      attr: 1, // 1: 私海 2: 公海
      ...unref(searchParams)
    })
    return {
      list: res.data.list,
      total: res.data.total
    }
  },
  fetchDelApi: async () => {
    const res = await delListApi(unref(ids).join(','))
    return !!res
  }
})
const { loading, dataList, total, currentPage, pageSize } = tableState
const { getList } = tableMethods

getList()

useEventBus({
  name: 'getList',
  callback: (type: string) => {
    if (type === 'add') {
      currentPage.value = 1
    }
    getList()
  }
})

const { t } = useI18n()

const allSchemas = ref<AllSchemas>({
  searchSchema: [], // 确保与接口匹配
  tableColumns: [], // 确保与接口匹配
  formSchema: [], // 确保与接口匹配
  detailSchema: [] // 确保与接口匹配
})
let customerFieldEnum: FieldEnum = {} // 根据实际数据初始化
const getFieldEnum = async () => {
  const res = await getFieldEnumApi({ field: 'all' })
  customerFieldEnum = res.data
}
let operatorOptions: SelectOption[] = []
const getOperatorSelectList = async () => {
  const res = await getOperatorSelectListApi({})
  operatorOptions = res.data.list
}
const fetchData = async () => {
  await getFieldEnum()
  await getOperatorSelectList()
  const schemaConfig: SchemaConfig = {
    // actionButtonsTemplate,
    operatorOptions,
    fieldEnum: customerFieldEnum
  }
  const { crudSchemas } = useSchema(schemaConfig)
  const { allSchemas: newSchemas } = useCrudSchemas(crudSchemas)

  // 合并新的数据，确保不改变原有数据的类型
  allSchemas.value = {
    searchSchema: newSchemas.searchSchema || [],
    tableColumns: newSchemas.tableColumns || []
  }
}
// End CRUD Template Block
// ------------------------

fetchData()

const AddAction = () => {
  push('/customer/customerList/add?source=/customerList')
}

const delLoading = ref(false)
</script>

<template>
  <ContentWrap>
    <Search
      v-if="allSchemas.searchSchema"
      :schema="allSchemas.searchSchema"
      @search="setSearchParams"
      @reset="setSearchParams"
    />

    <div class="mb-10px">
      <BaseButton type="primary" @click="AddAction">{{ t('action.add') }}</BaseButton>
      <!-- <BaseButton :loading="delLoading" type="danger" @click="delData(null)">
        {{ t('action.del') }}
      </BaseButton> -->
    </div>

    <Table
      showAction
      v-model:pageSize="pageSize"
      v-model:currentPage="currentPage"
      :columns="allSchemas.tableColumns"
      :data="dataList"
      :loading="loading"
      :pagination="{
        total: total
      }"
      @register="tableRegister"
    />
  </ContentWrap>
</template>
@/hooks/event/useEventBus
